package ex05.pyrmont.startup;

import ex05.pyrmont.core.SimpleLoaderV5;
import ex05.pyrmont.core.SimpleWrapperV5;
import ex05.pyrmont.valves.ClientIPLoggerValveV5;
import ex05.pyrmont.valves.HeaderLoggerValveV5;
import org.apache.catalina.Loader;
import org.apache.catalina.Pipeline;
import org.apache.catalina.Valve;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.http.HttpConnector;

/**
 * call by using    http://localhost:8080/ModernServlet, but could be invoked by any name
 *  http://localhost:8080/PrimitiveServlet
 * 这个 启动没有 使用 Mapper , 而是直接把 servlet  赋值;
 *
 * 使用  {@link  SimpleLoaderV5 }  作为类加载器 ,
 *
 */
public final class BootstrapV5_1 {




    public static void main(String[] args) {


        HttpConnector connector = new HttpConnector();
        Wrapper wrapper = new SimpleWrapperV5();
        wrapper.setServletClass("ModernServlet");
        Loader loader = new SimpleLoaderV5();
        wrapper.setLoader(loader);  // wrapper.loader


        Valve headerLoggerValveV5 = new HeaderLoggerValveV5();
        Valve clientIPLoggerValveV5 = new ClientIPLoggerValveV5();


        ((Pipeline) wrapper).addValve(headerLoggerValveV5);  //强制转成 pipeline 来添加 阀门
        ((Pipeline) wrapper).addValve(clientIPLoggerValveV5);

        connector.setContainer(wrapper);

        try {
            connector.initialize();
            connector.start();

            // make the application wait until we press a key.
            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
